package com.huika.renrench.control.my.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Response.Listener;
import com.google.gson.reflect.TypeToken;
import com.huika.renrench.GlobalApp;
import com.huika.renrench.R;
import com.huika.renrench.config.UrlConstants;
import com.huika.renrench.control.common.base.BaseFra;
import com.huika.renrench.control.investment.activity.BookInvestActivity;
import com.huika.renrench.http.FormResultRequest;
import com.huika.renrench.http.JSONWrapAjaxParams;
import com.huika.renrench.http.RequestResult;
import com.huika.renrench.support.bean.MySubscribean;

/**
 * @author yangyuan
 * @date：2015年6月3日
 * @describe:投资预约
 */
public class MySubscribeFra extends BaseFra{

	private TextView text_is_subscribe;
	private TextView text_ranking_subscribe;
	private TextView text_point_subscribe;
	
	private Button btn_manage_subscribe;
	
	@Override
	protected View inflaterView(LayoutInflater inflater, ViewGroup container,
			Bundle bundle) {
		return inflater.inflate(R.layout.my_subscribe_fra, container, false);
	}
	
	@Override
	protected void initWidget(View parentView) {
		super.initWidget(parentView);

		text_is_subscribe = (TextView) parentView.findViewById(R.id.text_is_subscribe);
		text_ranking_subscribe = (TextView) parentView.findViewById(R.id.text_ranking_subscribe);
		text_point_subscribe = (TextView) parentView.findViewById(R.id.text_point_subscribe);
		btn_manage_subscribe = (Button) parentView.findViewById(R.id.btn_manage_subscribe);
		
		//调网络
		showLoadingDialog("加载中");
		
		btn_manage_subscribe.setOnClickListener(this);
	}
	
	@Override
	public void onResume() {
		super.onResume();
		executeUpdateBackgInfoList();
	}
	
	@Override
	protected void widgetClick(View v) {
		super.widgetClick(v);
//		showToastMsg("管理预约");
		Intent intent = new Intent(getActivity(), BookInvestActivity.class);
		intent.putExtra("flag", 1);
		startActivity(intent);
	}
	
	/** 提交意见反馈*/
	private void executeUpdateBackgInfoList(){
		FormResultRequest<MySubscribean> request = new FormResultRequest<MySubscribean>(UrlConstants.GETRESERVEINVEST, getUpdateBackgResp, this, new TypeToken<RequestResult<MySubscribean>>() {}.getType());
		JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();//
		ajaxParams.putCommonTypeParam("userId", GlobalApp.getInstance().getUserId());//userId
		request.setRequestParams(ajaxParams);
		executeRequest(request);
	}
	
	private Listener<RequestResult<MySubscribean>> getUpdateBackgResp=new Listener<RequestResult<MySubscribean>>() {
		
		@Override
		public void onResponse(RequestResult<MySubscribean> result) {
			
			if (result.flag == 1) {
				setView(result.getRs());
			} else {
				showToastMsg(result.msg);
			}
			dismissLoadingDialog();
			if (mOverlayLayout != null)
				hideOverLay();
		}
	};
	
	private void setView(MySubscribean bean){
		if (bean==null) {
			showToastMsg("下载错误");
			return;
		}
		if (0 == bean.getReserveStatus()) {
			text_is_subscribe.setText("未预约");
		}else {
			text_is_subscribe.setText("预约成功");
		}
		text_ranking_subscribe.setText(bean.getMyOrder()+"/"+bean.getSumOrder()+"人");
		text_point_subscribe.setText(bean.getReserveRecord()+"分");
		
	}
	
}
